public class MaxSubArray {
    public static int maxSubArray(int[] nums) {
        int maxSum = nums[0];
        int currentSum = nums[0];
        
        for (int i = 1; i < nums.length; i++) {
            currentSum = Math.max(nums[i], currentSum + nums[i]);
            maxSum = Math.max(maxSum, currentSum);
        }
        
        return maxSum;
    }

    public static void main(String[] args) {
        int[] nums1 = {1, -2, 3, 5, -1};
        System.out.println("最大子数组和为: " + maxSubArray(nums1)); // 输出: 8

        int[] nums2 = {1, -2, 3, -8, 5, 1};
        System.out.println("最大子数组和为: " + maxSubArray(nums2)); // 输出: 6

        int[] nums3 = {1, -2, 3, -2, 5, 1};
        System.out.println("最大子数组和为: " + maxSubArray(nums3)); // 输出: 7
    }
}
